package com.sinochem.sales.service.gateways.impl;

import com.alibaba.fastjson.JSON;
import com.sinochem.sales.common.enums.ResultEnum;
import com.sinochem.sales.common.api.to.sms.SendMsg;
import com.sinochem.sales.common.api.to.sms.SendMsgResponse;
import com.sinochem.sales.service.gateways.SendSmsService;
import com.sinochem.sales.service.gateways.client.IILinkMsgService;
import com.sinochem.sales.service.gateways.client.ILinkMsgService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.xml.ws.Holder;
import java.net.MalformedURLException;
import java.net.URL;

@Service
public class SendSmsServiceImpl implements SendSmsService {

    private static Logger logger = Logger.getLogger(SendSmsServiceImpl.class);

    @Value("${sms.sysName}")
    private String sysName;
    @Value("${sms.sysPwd}")
    private String sysPwd;
    @Value("${sms.serverUrl}")
    private String serverUrl;

    /**
     * 发送短信信息
     *
     * @param msg 短信信息
     * @return
     */
    @Override
    public SendMsgResponse sendSmsMessage(SendMsg msg) {
        SendMsgResponse msgResponse = new SendMsgResponse();
        try {
            URL url = null;
            try {
                url = new URL(serverUrl + "/ILinkmsgService.svc?wsdl");
            } catch (MalformedURLException e) {
                java.util.logging.Logger.getLogger(ILinkMsgService.class.getName())
                        .log(java.util.logging.Level.INFO,
                                "Can not initialize the default wsdl from {0}", "http://172.16.104.157:8088/ILinkmsgService.svc?wsdl");
            }
            if (null == url) {
                msgResponse.setSendMsgResult("发送短信失败");
                msgResponse.setStrReturnCode(String.valueOf(ResultEnum.ERROR.getCode()));
                msgResponse.setStrReturnMsg("url 为空");
                return msgResponse;
            }
            ILinkMsgService msgService = new ILinkMsgService(url);
            IILinkMsgService port = msgService.getBasicHttpBindingIILinkMsgService();

            Holder<String> msgReturnResult = new Holder<>();
            Holder<String> msgReturnCode = new Holder<>();
            Holder<String> msgReturnMsg = new Holder<>();
            msg.setStrSendOrgCode("1002");
            msg.setStrSendOrgName("新壹化网");
            msg.setStrSendSysName(sysName);
            msg.setStrSysPwd(sysPwd);
            msg.setStrPriority("1");
            logger.info("fasong msg" + JSON.toJSONString(msg));
            port.sendMsg(msg.getStrSendSysName(), msg.getStrSysPwd(), msg.getStrMobileNo(), msg.getStrContent(),
                    msg.getStrPriority(), msg.getStrTiming(), msg.getStrSendOrgCode(), msg.getStrSendOrgName(),
                    msgReturnResult, msgReturnCode, msgReturnMsg);
            logger.info("发送短信至：---------------" + msg.getStrMobileNo() + "----------时间：" + msg.getStrTiming() + "-------");
            logger.info("发送短信的结果是：" + msgReturnResult.value + "------" + msgReturnCode.value + "-------" + msgReturnMsg.value);

            //不返回0发送失败
            if (!(msgReturnCode.value).equals(String.valueOf(ResultEnum.ERROR.getCode()))) {
                msgResponse.setSendMsgResult("发送短信失败");
                msgResponse.setStrReturnCode(String.valueOf(ResultEnum.ERROR.getCode()));
                msgResponse.setStrReturnMsg(msgReturnMsg.value);
                return msgResponse;
            }
            msgResponse.setSendMsgResult(msgReturnResult.value);
            msgResponse.setStrReturnCode(String.valueOf(ResultEnum.SUCCESS.getCode()));
            msgResponse.setStrReturnMsg(msgReturnMsg.value);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("短信内容:" + msg + "失败!");
            msgResponse.setSendMsgResult("发送短信失败");
            msgResponse.setStrReturnCode(String.valueOf(ResultEnum.ERROR.getCode()));
            msgResponse.setStrReturnMsg(e.getMessage());
        }
        return msgResponse;
    }
}
